package com.example;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TaughtCourseDAO {

    public List<TaughtCourse> getTaughtCoursesByTeacherUsername(String teacherUsername) {
        List<TaughtCourse> taughtCourses = new ArrayList<>();
        String sql = "SELECT id, teacherUsername, courseName, semester FROM taught_courses WHERE teacherUsername = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, teacherUsername);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                TaughtCourse course = new TaughtCourse(
                    rs.getInt("id"),
                    rs.getString("teacherUsername"),
                    rs.getString("courseName"),
                    rs.getString("semester")
                );
                taughtCourses.add(course);
            }
        } catch (SQLException e) {
            System.err.println("Error fetching taught courses for teacher " + teacherUsername + ": " + e.getMessage());
        }
        return taughtCourses;
    }
} 